package com.guyu.plugin.sms.core;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import org.dromara.sms4j.api.SmsBlend;
import org.dromara.sms4j.api.proxy.SmsProcessor;
import org.dromara.sms4j.comm.constant.NumberOfParasmeters;
import org.dromara.sms4j.comm.exception.SmsBlendException;

import java.lang.reflect.Method;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Objects;

/**
 * 黑名单前置拦截执行器
 */
public class GuyuBlackListProcessor implements SmsProcessor {

    private final GuyuBlackListDao blackListDao;

    public GuyuBlackListProcessor(GuyuBlackListDao blackListDao) {
        this.blackListDao = blackListDao;
    }

    @Override
    public int getOrder() {
        return 0;
    }

    @Override
    public Object[] preProcessor(Method method, Object source, Object[] param) {
        String name = method.getName();
        int parameterCount = method.getParameterCount();

        if ("sendMessage".equals(name)) {
            if (NumberOfParasmeters.TWO == NumberOfParasmeters.getNumberOfParasmetersEnum(parameterCount)) {
                sendMessagePreProcess(source, (String) param[0], param[1]);
                return param;
            }
            if (NumberOfParasmeters.THREE == NumberOfParasmeters.getNumberOfParasmetersEnum(parameterCount)) {
                if (null == param[2]) {
                    param[2] = new LinkedHashMap<>();
                }
                sendMessageByTemplatePreProcess(source, (String) param[0], (String) param[1], (LinkedHashMap<String, String>) param[2]);
                return param;
            }
        }
        if ("massTexting".equals(name)) {
            if (NumberOfParasmeters.TWO == NumberOfParasmeters.getNumberOfParasmetersEnum(parameterCount)) {
                massTextingPreProcess(source, (List<String>) param[0], (String) param[1]);
                return param;
            }
            if (NumberOfParasmeters.THREE == NumberOfParasmeters.getNumberOfParasmetersEnum(parameterCount)) {
                if (null == param[2]) {
                    param[2] = new LinkedHashMap<>();
                }
                massTextingByTemplatePreProcess(source, (List<String>) param[0], (String) param[1], (LinkedHashMap<String, String>) param[2]);
                return param;
            }
        }
        return param;
    }


    public void sendMessagePreProcess(Object source, String phone, Object message) {
        doRestricted(source, phone);
    }

    public void sendMessageByTemplatePreProcess(Object source, String phone, String templateId, LinkedHashMap<String, String> messages) {
        doRestricted(source, phone);
    }

    public void massTextingPreProcess(Object source, List<String> phones, String message) {
        doRestricted(source, phones);
    }

    public void massTextingByTemplatePreProcess(Object source, List<String> phones, String templateId, LinkedHashMap<String, String> messages) {
        doRestricted(source, phones);
    }

    public void doRestricted(Object source, String phone) {
        if (Objects.isNull(blackListDao)) {
            return;
        }
        //根据模板查询黑名单
        String configId = ((SmsBlend) source).getConfigId();
        boolean result = blackListDao.check(configId, phone);
        if (result) {
            throw new SmsBlendException("The phone in the blacklist！[" + phone + "]");
        }
    }

    public void doRestricted(Object source, List<String> phones) {
        if (Objects.isNull(blackListDao)) {
            return;
        }
        //根据模板查询黑名单
        String configId = ((SmsBlend) source).getConfigId();
        List<String> blackList = blackListDao.check(configId, phones);
        if (CollectionUtil.isNotEmpty(blackList)) {
            throw new SmsBlendException("The phone in the blacklist！[" + StrUtil.join(",", blackList) + "]");
        }
    }

}
